{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:06:31.048564400Z",
     "start_time": "2024-07-16T02:06:30.764603600Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "a  1    1.778811\n   2   -1.261310\n   3    0.375463\nb  1   -0.177749\n   3    0.153174\nc  1   -2.560898\n   2    1.300364\nd  2   -1.116715\n   3    0.358775\ndtype: float64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.Series(np.random.randn(9),index=[list('aaabbccdd'),\n",
    "                                           [1,2,3,1,3,1,2,2,3]])\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:08:11.347904900Z",
     "start_time": "2024-07-16T02:08:11.339804600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "1    1.778811\n2   -1.261310\n3    0.375463\ndtype: float64"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['a']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:09:10.361853900Z",
     "start_time": "2024-07-16T02:09:10.352517500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "b  1   -0.177749\n   3    0.153174\nc  1   -2.560898\n   2    1.300364\ndtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['b':'c']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:09:30.524562100Z",
     "start_time": "2024-07-16T02:09:30.514058200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "b  1   -0.177749\n   3    0.153174\nd  2   -1.116715\n   3    0.358775\ndtype: float64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[['b','d']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:09:59.509089200Z",
     "start_time": "2024-07-16T02:09:59.500126700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "a   -1.261310\nc    1.300364\nd   -1.116715\ndtype: float64"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[:,2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:10:52.063754300Z",
     "start_time": "2024-07-16T02:10:52.054719800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "   a  b    c  d\n0  0  7  one  0\n1  1  6  one  1\n2  2  5  one  2\n3  3  4  two  0\n4  4  3  two  1\n5  5  2  two  2\n6  6  1  two  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n      <td>one</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n      <td>one</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n      <td>one</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>4</td>\n      <td>two</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>3</td>\n      <td>two</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>5</td>\n      <td>2</td>\n      <td>two</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>6</td>\n      <td>1</td>\n      <td>two</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame=pd.DataFrame({'a':range(7),'b':range(7,0,-1),\n",
    "                    'c':['one','one','one','two','two','two','two'],\n",
    "                    'd':[0,1,2,0,1,2,3]})\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:14:36.090038Z",
     "start_time": "2024-07-16T02:14:36.082821200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "       a  b\nc   d      \none 0  0  7\n    1  1  6\n    2  2  5\ntwo 0  3  4\n    1  4  3\n    2  5  2\n    3  6  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n    </tr>\n    <tr>\n      <th>c</th>\n      <th>d</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">one</th>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">two</th>\n      <th>0</th>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2=frame.set_index(['c','d'])\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:16:03.126199400Z",
     "start_time": "2024-07-16T02:16:03.117173200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "       a  b    c  d\nc   d              \none 0  0  7  one  0\n    1  1  6  one  1\n    2  2  5  one  2\ntwo 0  3  4  two  0\n    1  4  3  two  1\n    2  5  2  two  2\n    3  6  1  two  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n    <tr>\n      <th>c</th>\n      <th>d</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">one</th>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n      <td>one</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n      <td>one</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n      <td>one</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">two</th>\n      <th>0</th>\n      <td>3</td>\n      <td>4</td>\n      <td>two</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>3</td>\n      <td>two</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5</td>\n      <td>2</td>\n      <td>two</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>6</td>\n      <td>1</td>\n      <td>two</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.set_index(['c','d'],drop=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:17:36.596966700Z",
     "start_time": "2024-07-16T02:17:36.585553500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "     c  d  a  b\n0  one  0  0  7\n1  one  1  1  6\n2  one  2  2  5\n3  two  0  3  4\n4  two  1  4  3\n5  two  2  5  2\n6  two  3  6  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>c</th>\n      <th>d</th>\n      <th>a</th>\n      <th>b</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>one</td>\n      <td>0</td>\n      <td>0</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>one</td>\n      <td>1</td>\n      <td>1</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>one</td>\n      <td>2</td>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>two</td>\n      <td>0</td>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>two</td>\n      <td>1</td>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>two</td>\n      <td>2</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>two</td>\n      <td>3</td>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:18:27.382288400Z",
     "start_time": "2024-07-16T02:18:27.377750800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "数据链接"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "left = pd.DataFrame({'key': ['Ko','K1','K2','K3'],\n",
    "                     'A': ['AO','A1','A2','A3'],\n",
    "                     'B': ['BO','B1','B2','B3']})\n",
    "right = pd. DataFrame({'key': ['Ko', 'K1','K2','K3'],\n",
    "                       'C':['C0','C1','C2','C3'],\n",
    "                       'D':['D0','D1','D2','D3']})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:26:55.334665900Z",
     "start_time": "2024-07-16T02:26:55.316436600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "  key   A   B\n0  Ko  AO  BO\n1  K1  A1  B1\n2  K2  A2  B2\n3  K3  A3  B3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ko</td>\n      <td>AO</td>\n      <td>BO</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>K1</td>\n      <td>A1</td>\n      <td>B1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>K2</td>\n      <td>A2</td>\n      <td>B2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>K3</td>\n      <td>A3</td>\n      <td>B3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:26:56.408018800Z",
     "start_time": "2024-07-16T02:26:56.399234200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "  key   C   D\n0  Ko  C0  D0\n1  K1  C1  D1\n2  K2  C2  D2\n3  K3  C3  D3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ko</td>\n      <td>C0</td>\n      <td>D0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>K1</td>\n      <td>C1</td>\n      <td>D1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>K2</td>\n      <td>C2</td>\n      <td>D2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>K3</td>\n      <td>C3</td>\n      <td>D3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:27:29.228769600Z",
     "start_time": "2024-07-16T02:27:29.209602900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "  key   A   B   C   D\n0  Ko  AO  BO  C0  D0\n1  K1  A1  B1  C1  D1\n2  K2  A2  B2  C2  D2\n3  K3  A3  B3  C3  D3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ko</td>\n      <td>AO</td>\n      <td>BO</td>\n      <td>C0</td>\n      <td>D0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>K1</td>\n      <td>A1</td>\n      <td>B1</td>\n      <td>C1</td>\n      <td>D1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>K2</td>\n      <td>A2</td>\n      <td>B2</td>\n      <td>C2</td>\n      <td>D2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>K3</td>\n      <td>A3</td>\n      <td>B3</td>\n      <td>C3</td>\n      <td>D3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left,right)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T02:43:31.164287600Z",
     "start_time": "2024-07-16T02:43:31.137052200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "# 处理重复名\n",
    "df_obj1=pd.DataFrame({'key':list('bbacaab'),\n",
    "                      'data': np.random.randint(0,10,7)})\n",
    "df_obj2=pd.DataFrame({'key':list('abd'),\n",
    "                      'data': np.random.randint(0,10,3)})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:19:17.524642500Z",
     "start_time": "2024-07-16T03:19:17.517017800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data\n",
      "0   b     5\n",
      "1   b     6\n",
      "2   a     9\n",
      "3   c     7\n",
      "4   a     1\n",
      "5   a     8\n",
      "6   b     4\n"
     ]
    },
    {
     "data": {
      "text/plain": "  key  data\n0   a     9\n1   b     6\n2   d     8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>d</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(df_obj1)\n",
    "df_obj2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:23:07.414148600Z",
     "start_time": "2024-07-16T03:23:07.401789100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data_left  data_right\n",
      "0   b          5           6\n",
      "1   b          6           6\n",
      "2   a          9           9\n",
      "3   a          1           9\n",
      "4   a          8           9\n",
      "5   b          4           6\n"
     ]
    }
   ],
   "source": [
    "print(pd.merge(df_obj1,df_obj2,on='key',\n",
    "               suffixes=('_left','_right')))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:20:59.372684Z",
     "start_time": "2024-07-16T03:20:59.363567500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Join"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [],
   "source": [
    "left2=pd.DataFrame([[1.,2.],[3,4],[5,6]],index=['a','c','e'],\n",
    "                   columns=['语文','数学'])\n",
    "right2=pd.DataFrame([[7.,8.],[9,10],[11,12],[13,14]],\n",
    "                    index=list('bcde'),\n",
    "                   columns=['英语','综合'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:33:32.807427100Z",
     "start_time": "2024-07-16T03:33:32.798910Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "    语文   数学\na  1.0  2.0\nc  3.0  4.0\ne  5.0  6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:32:10.422954400Z",
     "start_time": "2024-07-16T03:32:10.400508700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "     英语    综合\nb   7.0   8.0\nc   9.0  10.0\nd  11.0  12.0\ne  13.0  14.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>英语</th>\n      <th>综合</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>b</th>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:33:36.442024Z",
     "start_time": "2024-07-16T03:33:36.432502900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "    语文   数学    英语    综合\na  1.0  2.0   NaN   NaN\nb  NaN  NaN   7.0   8.0\nc  3.0  4.0   9.0  10.0\nd  NaN  NaN  11.0  12.0\ne  5.0  6.0  13.0  14.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>综合</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left2,right2,how=\"outer\",left_index=True,\n",
    "         right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:36:51.111804400Z",
     "start_time": "2024-07-16T03:36:51.106546300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "    语文   数学    英语    综合\na  1.0  2.0   NaN   NaN\nb  NaN  NaN   7.0   8.0\nc  3.0  4.0   9.0  10.0\nd  NaN  NaN  11.0  12.0\ne  5.0  6.0  13.0  14.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>综合</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# join方法按照索引进行合并，不能出现重叠的列名\n",
    "left2.join(right2,how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T03:38:37.622936200Z",
     "start_time": "2024-07-16T03:38:37.495159200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "pd.concat"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [],
   "source": [
    "df1=pd.DataFrame(np.arange(6).reshape(3,2),index=list('abc'),\n",
    "                 columns=['one','two'])\n",
    "df2=pd.DataFrame(np.arange(4).reshape(2,2)+5,index=list('ac'),\n",
    "                 columns=['one','two'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:39:06.879308700Z",
     "start_time": "2024-07-16T06:39:06.871291700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "   one  two\na    0    1\nb    2    3\nc    4    5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:36:55.961251400Z",
     "start_time": "2024-07-16T06:36:55.954813800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "   three  four\na      5     6\nc      7     8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:36:58.015412900Z",
     "start_time": "2024-07-16T06:36:58.009663900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "   one  two\na    0    1\nb    2    3\nc    4    5\na    5    6\nc    7    8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>a</th>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df1,df2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:39:09.512365100Z",
     "start_time": "2024-07-16T06:39:09.502337100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "   one  two  one  two\na    0    1  5.0  6.0\nb    2    3  NaN  NaN\nc    4    5  7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df1,df2],axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:39:11.659923Z",
     "start_time": "2024-07-16T06:39:11.650473900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "重塑和轴向旋转"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 重塑层次化索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "科目  语文  数学  英语\n",
      "姓名            \n",
      "老王   0   1   2\n",
      "小刘   3   4   5\n"
     ]
    }
   ],
   "source": [
    "data=pd.DataFrame(np.arange(6).reshape(2,3),\n",
    "                  index=pd.Index(['老王','小刘'],name='姓名'),\n",
    "                  columns=pd.Index(['语文','数学','英语'],\n",
    "                                   name='科目'))\n",
    "print(data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:44:30.890997100Z",
     "start_time": "2024-07-16T06:44:30.879752400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "姓名  科目\n",
      "老王  语文    0\n",
      "    数学    1\n",
      "    英语    2\n",
      "小刘  语文    3\n",
      "    数学    4\n",
      "    英语    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "r=data.stack()\n",
    "print(type(r))\n",
    "print(r)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:45:11.524365500Z",
     "start_time": "2024-07-16T06:45:11.516123400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "科目  语文  数学  英语\n姓名            \n老王   0   1   2\n小刘   3   4   5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>科目</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n    <tr>\n      <th>姓名</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>老王</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>小刘</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.unstack(1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:46:36.076903500Z",
     "start_time": "2024-07-16T06:46:36.071266300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "姓名  老王  小刘\n科目        \n语文   0   3\n数学   1   4\n英语   2   5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>姓名</th>\n      <th>老王</th>\n      <th>小刘</th>\n    </tr>\n    <tr>\n      <th>科目</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>语文</th>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>数学</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>英语</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.unstack('姓名')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T06:54:59.316799200Z",
     "start_time": "2024-07-16T06:54:59.301689700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "轴向旋转"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "data": {
      "text/plain": "         date class  values\n0  2018-11-22     a       5\n1  2018-11-22     b       3\n2  2018-11-23     b       2\n3  2018-11-23     c       6\n4  2018-11-24     c       1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>date</th>\n      <th>class</th>\n      <th>values</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2018-11-22</td>\n      <td>a</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2018-11-22</td>\n      <td>b</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2018-11-23</td>\n      <td>b</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2018-11-23</td>\n      <td>c</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2018-11-24</td>\n      <td>c</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = pd.DataFrame({'date': ['2018-11-22','2018-11-22','2018-11-23','2018-11-23','2018-11-24'],'class': ['a' ,'b','b','c','c' ],'values': [5, 3, 2, 6, 1]},columns=['date','class','values'])\n",
    "df3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T07:12:24.893208900Z",
     "start_time": "2024-07-16T07:12:24.884574300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "data": {
      "text/plain": "class         a    b    c\ndate                     \n2018-11-22  5.0  3.0  NaN\n2018-11-23  NaN  2.0  6.0\n2018-11-24  NaN  NaN  1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>class</th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-11-22</th>\n      <td>5.0</td>\n      <td>3.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2018-11-23</th>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>2018-11-24</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.pivot(index='date',columns='class',values='values')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T07:14:58.762418600Z",
     "start_time": "2024-07-16T07:14:58.750060400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "data": {
      "text/plain": "           values          \nclass           a    b    c\ndate                       \n2018-11-22    5.0  3.0  NaN\n2018-11-23    NaN  2.0  6.0\n2018-11-24    NaN  NaN  1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">values</th>\n    </tr>\n    <tr>\n      <th>class</th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-11-22</th>\n      <td>5.0</td>\n      <td>3.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2018-11-23</th>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>2018-11-24</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.set_index(['date','class']).unstack('class')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T07:16:06.294813200Z",
     "start_time": "2024-07-16T07:16:06.276387500Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
